// import data from '../../messageManager/messageAdd/data';
import apis from '../services/AddSourceApi';

const { queryByName, queryByOrg, queryLabelByName, queryUserLabel } = apis;

const initData = {
  loading: false,
  pageNum: 1,
  pageSize: 5,
  datas: [],
  content: '',
  orgCode: '',
  userName: '',
  totalCount: 0,
  totalPage: 0,
  query: 'name'
};

export default {
  namespace: 'personSelectModel',
  state: initData,
  effects: {
    // 查看用户标签
    *queryUserLabel({ payload }, { select, call, put }) {
      const { userLabel, content, isPagination, pageNum = 1 } = payload;
      const modelCacheParam = yield select(state => state.personSelectModel);
      const cont = modelCacheParam.content;
      yield put({ type: 'dump', payload: { loading: true } });

      const { datas = [], totalPage = 0, totalCount = 0 } = yield call(
        queryUserLabel,
        {
          userLabel,
          content: isPagination ? cont : content,
          pageNum
        }
      );

      yield put({
        type: 'dump',
        payload: {
          content,
          pageNum,
          datas,
          totalPage,
          totalCount,
          loading: false
        }
      });
    },
    // 新增标签、使用对象
    *queryLabelByName({ payload }, { select, call, put }) {
      const { isSearch, isOrg, isPagination, pageNum = 1 } = payload;
      let { orgCode, content } = payload;
      const modelCacheParam = yield select(state => state.personSelectModel);
      const code = modelCacheParam.orgCode;
      const cont = modelCacheParam.content;
      yield put({ type: 'dump', payload: { loading: true } });
      let result = {};
      if (isSearch) {
        // 搜索
        result = yield call(queryLabelByName, {
          orgCode: '',
          content,
          pageNum
        });
        orgCode = '';
      }

      if (isOrg) {
        // 组织树
        result = yield call(queryLabelByName, {
          orgCode,
          content: '',
          pageNum
        });
        content = '';
      }

      if (isPagination) {
        // 分页
        result = yield call(queryLabelByName, {
          orgCode: code,
          content: cont,
          pageNum
        });
        orgCode = code;
        content = cont;
      }

      const { datas = [], totalPage = 0, totalCount = 0 } = result;
      yield put({
        type: 'dump',
        payload: {
          orgCode,
          content,
          pageNum,
          datas,
          totalPage,
          totalCount,
          loading: false
        }
      });
    },
    *queryByName({ payload }, { call, put }) {
      yield put({ type: 'triggerLoading', payload: true });
      const { datas = [], totalPage = 0, totalCount = 0 } = yield call(
        queryByName,
        payload
      );

      yield put({
        type: 'saveData',
        payload: { datas, totalPage, totalCount }
      });
      yield put({ type: 'triggerLoading', payload: false });
      yield put({ type: 'changeQuery', payload: 'name' });
      yield put({
        type: 'saveFilter',
        payload: { content: payload.content || '', pageNum: 1 }
      });
    },
    *queryByOrg({ payload: { orgCode = '', userName = '' } }, { call, put }) {
      yield put({ type: 'triggerLoading', payload: true });
      const { datas = [], totalPage = 0, totalCount = 0 } = yield call(
        queryByOrg,
        orgCode ? { orgCode, userName } : { userName }
      );
      yield put({
        type: 'saveData',
        payload: { datas, totalPage, totalCount }
      });
      yield put({ type: 'triggerLoading', payload: false });
      yield put({ type: 'changeQuery', payload: 'org' });
      yield put({
        type: 'saveFilter',
        payload: {
          orgCode: orgCode || '',
          userName: userName || '',
          pageNum: 1
        }
      });
    },
    *query({ payload }, { call, put, select }) {
      const queryWay = yield select(
        ({ personSelectModel: { query } }) => query
      );
      let params;
      let resData;
      yield put({ type: 'triggerLoading', payload: true });
      if (queryWay === 'name') {
        const contentCache = yield select(
          ({ personSelectModel: { content } }) => content
        );
        params = { content: contentCache, pageNum: payload };
        resData = yield call(queryByName, params);
      } else {
        const orgCodeCache = yield select(
          ({ personSelectModel: { orgCode } }) => orgCode
        );
        const userNameCache = yield select(
          ({ personSelectModel: { userName } }) => userName
        );
        params = orgCodeCache
          ? {
              orgCode: orgCodeCache,
              userName: userNameCache,
              pageNum: payload
            }
          : {
              userName: userNameCache,
              pageNum: payload
            };
        resData = yield call(queryByOrg, params);
      }
      const { datas = [], totalPage, totalCount } = resData;
      yield put({
        type: 'saveData',
        payload: { datas, totalPage, totalCount }
      });
      yield put({ type: 'triggerLoading', payload: false });
      yield put({ type: 'saveFilter', payload: { pageNum: payload } });
    }
  },
  reducers: {
    saveData: (state, { payload }) => ({ ...state, ...payload }),
    saveFilter: (state, { payload }) => ({ ...state, ...payload }),
    changeQuery: (state, { payload }) => ({ ...state, query: payload }),
    triggerLoading: (state, { payload }) => ({ ...state, loading: payload }),
    dump: (state, { payload }) => ({ ...state, ...payload }),
    clearData: () => ({ ...initData })
  }
};
